﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Gift.DAL;
using Gift.Model;

namespace WebSite.admin.company
{
    public partial class OutInDetail : BaseAdmin
    {
        readonly dalCompany theCompany = new dalCompany();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindGridView();
                try
                {
                    string sMoney = theCompany.GetCompanyMoney(Request.QueryString["id"]).ToString();
                    this.divMoney.InnerHtml = string.Format("公司余额:{0}元,人民币大写:{1}", sMoney, Gift.Utile.MathLibrary.CmycurD(decimal.Parse(sMoney)));
                }
                catch (Exception ex)
                {
                    LogFile.Write(ELogType.Error, ex.Message);
                }
            }
        }

        protected void PagerButtonClick(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
            this.BindGridView();
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;
            this.BindGridView();
        }

        void BindGridView()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                string sWhere = string.Empty;

                if (!string.IsNullOrEmpty(this.txtSartTime.Value.Trim()))
                {
                    sWhere = string.Format(" and OperateTime >= convert(datetime,'{0}')", this.txtSartTime.Value);
                }

                if (!string.IsNullOrEmpty(this.txtEndTime.Value.Trim()))
                {
                    sWhere = sWhere + string.Format(" and convert(datetime,convert(varchar(10),OperateTime,121)) <= convert(datetime, '{0}')", this.txtEndTime.Value);
                }

                DataSet ds = theCompany.GetInOutList(Request.QueryString["id"],sWhere,this.rblInOutType.SelectedItem.Value);
                DataTable dt = new DataTable();
                if (ds != null)
                {
                    dt = ds.Tables[0];

                    dt.DefaultView.RowFilter = sWhere;
                    dt.DefaultView.Sort = "Id asc, OperateTime desc";
                    this.GridView1.DataSource = dt.DefaultView;
                    this.GridView1.DataBind();
                }
                 
              

                this.LblCurrentIndex.Text = "当前第 " + ((this.GridView1.PageIndex + 1)).ToString() + "页";
                this.LblPageCount.Text = "共 " + this.GridView1.PageCount.ToString() + "页";
                this.LblRecordCount.Text = "总计 " + dt.Rows.Count.ToString() + "个记录";
                if (dt.Rows.Count == 0)
                {
                    this.btnFirst.Visible = false;
                    this.btnPrev.Visible = false;
                    this.btnNext.Visible = false;
                    this.btnLast.Visible = false;
                    this.LblCurrentIndex.Visible = false;
                    this.LblPageCount.Visible = false;
                    this.LblRecordCount.Visible = false;
                }
                else if (this.GridView1.PageCount == 1)
                {
                    this.btnFirst.Visible = false;
                    this.btnPrev.Visible = false;
                    this.btnNext.Visible = false;
                    this.btnLast.Visible = false;
                }
                this.btnFirst.CommandName = "1";
                this.btnPrev.CommandName = (this.GridView1.PageIndex == 0) ? "1" : this.GridView1.PageIndex.ToString();
                this.btnNext.CommandName = (this.GridView1.PageCount == 1) ? this.GridView1.PageCount.ToString() : ((this.GridView1.PageIndex + 2)).ToString();
                this.btnLast.CommandName = this.GridView1.PageCount.ToString();
                this.ddlCurrentPage.Items.Clear();
                if (this.GridView1.PageCount > 0)
                {
                    for (int i = 1; i <= this.GridView1.PageCount; i++)
                    {
                        this.ddlCurrentPage.Items.Add(i.ToString());
                    }
                    this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex;
                }
            }
        }


        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if ((e.Row.RowType == DataControlRowType.Header) || (e.Row.RowType == DataControlRowType.DataRow))
            {             
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=\"" + e.Row.Style["BACKGROUND-COLOR"] + "\"");
                        e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor=\"#efefef\"");
                    } 
            }
        }

        protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
        {
            this.BindGridView();
        }

    }
}